package com.task;

import com.dto.UnloadGoodsDTO;
import com.entity.Employee;
import com.entity.GoodsEntity;
import com.mapper.EmployeeMapper;
import com.mapper.GoodsEntityMapper;
import com.mapper.UnloadGoodsMapper;
import com.service.GoodsEntityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: xuan
 * @Date: 2024/7/3  10:37
 * @Version: 1.0
 */
//@Component
@Slf4j
public class GoodsTimedTasks {
    @Autowired
    UnloadGoodsMapper unloadGoodsMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Scheduled(cron ="*/120 * * * * ?")//120秒执行一次
    public void UnloadGoodsReview() {
     //查到入库记录中带审核的信息，发送到对应的人用来提醒。
        List<UnloadGoodsDTO> unloadGoodsDTOS = unloadGoodsMapper.selectAll(null);
        List<UnloadGoodsDTO> reviewlist = new ArrayList<>();
        for (UnloadGoodsDTO unloadGoodsDTO : unloadGoodsDTOS) {
            //待审核出来后及时加入
            if("待审核".equals(unloadGoodsDTO.getIsApprove())){
                reviewlist.add(unloadGoodsDTO);
            }
        }
        //启用rabbitmq,把发起审核的人（对接人）,预计入库时间，审核人，联系方式，商品名称，进行编辑模板发送
        //String exchange, 交换机
        // String routingKey, 路由
        // Object message,消息体
        for (UnloadGoodsDTO unloadGoodsDTO : reviewlist) {
            String reviewmsg = unloadGoodsDTO.getApproveManEmail()+"-"+unloadGoodsDTO.getApproveMan()+" 同志您好,需要上线审核 "+unloadGoodsDTO.getIOEmployee()+"同志的入库申请。" +
                    "商品名称："+unloadGoodsDTO.getGname()+" 预计入库时间为："+unloadGoodsDTO.getStorageTime();
            rabbitTemplate.convertAndSend("","queueReview",reviewmsg);
        }
        log.info("审核消息推送已经执行");
    }
    @Autowired
    GoodsEntityMapper goodsEntityMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Scheduled(cron ="0 0 10,14,16 * * ?")//每天上午10点，下午2点，4点执行一次
    public void GoodsNumWarning() {
        List<GoodsEntity> goodsEntityList = goodsEntityMapper.selectAll(null);
        List<Employee> employees = employeeMapper.findAll();
        for (GoodsEntity goodsEntity : goodsEntityList) {
            //库存小于预警值
            Boolean flag = goodsEntity.getSum() <= goodsEntity.getWarningNumber();
            if(flag){
                for (Employee employee : employees) {
                    if(employee.getRid().equals(1)){
                        //给管理员发消息
                        //queueNumWarning
                        rabbitTemplate.convertAndSend("","queueNumWarning",employee.getEmail()+"-库存水平低于阈值，货物："+goodsEntity.getGname()+"  当前值："+goodsEntity.getSum()+"   阈值："+goodsEntity.getWarningNumber());

                    }
                }
            }
        }
    }

}
